home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d16 / winvn060.arc / UUCODE.C < prev    next >
Text File  |  1991-07-01  |  5KB  |  120 lines

  1. /*--- uucode.c -- File containing uuencode/uudecode routines.
  2.  *
  3.  *  Adapted from Berkeley code by Mark Riordan   12 August 1990.
  4.  */
  5.  
  6. int uuencode(unsigned char *bufin,unsigned int nbytes,
  7.   unsigned char *bufcoded);
  8. int uudecode(unsigned char *bufcoded,unsigned char *bufplain);
  9.  
  10. /*--- function uuencode -----------------------------------------------
  11.  *
  12.  *   Encode a single line of binary data to a standard format that
  13.  *   uses only printing ASCII characters (but takes up 33% more bytes).
  14.  *
  15.  *    Entry    bufin    points to a buffer of bytes.
  16.  *             nbytes   is the number of bytes in that buffer.
  17.  *                      This cannot be more than 62, and
  18.  *                      by convention it should be no more than 45.
  19.  *             bufcoded points to an output buffer.  Be sure that this
  20.  *                      can hold at least 2 + (4*nbytes)/3 characters.
  21.  *
  22.  *    Exit     bufcoded contains the coded line.  The first byte
  23.  *                      contains the (encoded) number of bytes that
  24.  *                      were encoded.  The following 4*nbytes/3 bytes
  25.  *                      contain printing ASCII characters representing
  26.  *                      those binary bytes, followed by a zero byte.
  27.  *             Returns the number of ASCII characters in "bufcoded".
  28.  */
  29. int
  30. uuencode(bufin, nbytes, bufcoded)
  31. unsigned char *bufin;
  32. unsigned int nbytes;
  33. unsigned char *bufcoded;
  34. {
  35. /* ENC is the basic 1 character encoding function to make a char printing */
  36. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  37.  
  38.    register unsigned char *outptr = bufcoded;
  39.    register unsigned char *inptr  = bufin;
  40.    int i;
  41.  
  42.    *(outptr++) = ENC(nbytes);
  43.  
  44.    for (i=0; i<nbytes; i += 3) {
  45.       *(outptr++) = ENC(*bufin >> 2);            /* c1 */
  46.       *(outptr++) = ENC((*bufin << 4) & 060 | (bufin[1] >> 4) & 017);  /* c2 */
  47.       *(outptr++) = ENC((bufin[1] << 2) & 074 | (bufin[2] >> 6) & 03); /* c3 */
  48.       *(outptr++) = ENC(bufin[2] & 077);         /* c4 */
  49.  
  50.       bufin += 3;
  51.    }
  52.    *outptr = '\0';
  53.    return(outptr - bufcoded);
  54. }
  55.  
  56. /*--- function uudecode ------------------------------------------------
  57.  *
  58.  *  Decode an ASCII-encoded buffer back to its original binary form.
  59.  *
  60.  *    Entry    bufcoded    points to a uuencoded string.  The first
  61.  *                         character of this string is the encoded
  62.  *                         number of original bytes in the buffer.
  63.  *                         (Can't be more than 62.)
  64.  *             bufplain    points to the output buffer; must be big
  65.  *                         enough to hold the decoded string (generally
  66.  *                         shorter than the encoded string).
  67.  *             Both buffers may be overrun a bit (less than 4 bytes)
  68.  *             during the decoding process, so leave a room for
  69.  *             a few extra bytes at the end.
  70.  *
  71.  *    Exit     Returns the number of binary bytes decoded.
  72.  *             bufplain    contains these bytes.
  73.  */
  74. int
  75. uudecode(bufcoded,bufplain)
  76. unsigned char *bufcoded;
  77. unsigned char *bufplain;
  78. {
  79. /* single character decode */
  80. #define DEC(c)    (((c) - ' ') & 077)
  81.  
  82.    int nbytes, nbytes2;
  83.    register unsigned char *bufin = bufcoded;
  84.    register unsigned char *bufout = bufplain;
  85.  
  86.    nbytes2 = nbytes = DEC(bufcoded[0]);
  87.  
  88.    bufin = &bufcoded[1];
  89.    while (nbytes > 0) {
  90.       *(bufout++) = DEC(*bufin) << 2 | DEC(bufin[1]) >> 4;
  91.       *(bufout++) = DEC(bufin[1]) << 4 | DEC(bufin[2]) >> 2;
  92.       *(bufout++) = DEC(bufin[2]) << 6 | DEC(bufin[3]);
  93.       bufin += 4;
  94.       nbytes -= 3;
  95.    }
  96.    return(nbytes2);
  97. }
  98.  
  99.  
  100. /*
  101.  * Copyright (c) 1983 Regents of the University of California.
  102.  * All rights reserved.
  103.  *
  104.  * Redistribution and use in source and binary forms are permitted
  105.  * provided that the above copyright notice and this paragraph are
  106.  * duplicated in all such forms and that any documentation,
  107.  * advertising materials, and other materials related to such
  108.  * distribution and use acknowledge that the software was developed
  109.  * by the University of California, Berkeley.  The name of the
  110.  * University may not be used to endorse or promote products derived
  111.  * from this software without specific prior written permission.
  112.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  113.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  114.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  115.  */
  116.  
  117. #if 0
  118. static char sccsid[] = "@(#)uuencode.c    5.6 (Berkeley) 7/6/88";
  119. #endif /* not lint */
  120.